package com.d6game.purchase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.d6game.purchase.model.Color;
import com.d6game.purchase.model.OrderViewHolder;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class OrderSettingAdapter extends BaseAdapter {
	private final String TAG = "OrderSettingAdapter";
	
	private Context context;
	private List<Color> list;
	private LayoutInflater listContainer;
	private ArrayList<Boolean> isChecked;
	private HashMap<Integer, OrderViewHolder> orderMap = new HashMap<Integer, OrderViewHolder>();;
	

	public OrderSettingAdapter(Context context, List<Color> list){
		this.context = context;
		this.list = list;
		this.listContainer = LayoutInflater.from(context);
		this.isChecked = new ArrayList<Boolean>();
		for (int i = 0; i< getCount(); i++) {
			isChecked.add(i, false);
		}
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	public void setColorList(List<Color> list){
		this.list = list;
	}
	public void checkedChange(int checkedID){
		isChecked.add(checkedID, !hasChecked(checkedID));
	}
	public boolean hasChecked(int checkedID){
		return isChecked.get(checkedID);
	}
	public void addCheckd(){
		isChecked.add(false);
	}
	public HashMap<Integer, OrderViewHolder> getOrderMap(){
		return orderMap;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int selectID= position;
		OrderViewHolder orderViewHolder = null;
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.order_setting, null);
			orderViewHolder = new OrderViewHolder();
			orderViewHolder.colorname = (TextView)convertView.findViewById(R.id.textViewOrderColorName);
			orderViewHolder.count = (EditText) convertView.findViewById(R.id.editTextOrderCount);
			orderViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxOrder);
			convertView.setTag(orderViewHolder);
		} else {
			orderViewHolder = (OrderViewHolder) convertView.getTag();
		}
		orderViewHolder.colorname.setText(list.get(position).getName());
		orderViewHolder.checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				checkedChange(selectID);
			}
		});
		if(!orderMap.containsKey(position)){
			orderMap.put(position, orderViewHolder);
		}
		return convertView;
	}
}
